import Vue from 'vue'
import VueRouter from 'vue-router'

import { getCookie } from '@/utils/cookie'
import store from '@/store'

import AdminIndex from '@/views/AdminIndex'
import AdminHome from '@/views/AdminHome'
import AdminLogin from '@/views/AdminLogin'
import NotFound from '@/components/NotFound'

import AuthAdd from '@/views/auth/AuthAdd'
import AuthList from '@/views/auth/AuthList'

import UserAdd from '@/views/user/UserAdd'
import UserList from '@/views/user/UserList'

import RoleList from '@/views/role/RoleList'
import RoleAdd from '@/views/role/RoleAdd'
import RoleEdit from '@/views/role/RoleEdit'

import CatList from '@/views/cat/CatList'

import DishList from '@/views/dish/DishList'
import DishAdd from '@/views/dish/DishAdd'
import DishEdit from '@/views/dish/DishEdit'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: AdminIndex,
    redirect: '/home'
  },
  {
    path: '/home',
    component: AdminHome,
    children: [
      {
        path: '/auth/add',
        component: AuthAdd
      },
      {
        path: '/auth/list',
        component: AuthList
      },
      {
        path: '/user/add',
        component: UserAdd
      },
      {
        path: '/user/list',
        component: UserList
      },
      {
        path: '/role/list',
        component: RoleList
      },
      {
        path: '/role/add',
        component: RoleAdd
      },
      {
        path: '/role/edit/:id',
        component: RoleEdit
      },
      {
        path: '/cat/list',
        component: CatList
      },
      {
        path: '/dish/list',
        component: DishList
      },
      {
        path: '/dish/add',
        component: DishAdd
      },
      {
        path: '/dish/edit',
        component: DishEdit
      }
    ]
  },
  {
    path: '/login',
    component: AdminLogin,
  },
  {
    path: '/404',
    component: NotFound
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  routes
})

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 路由守卫 - 判断用户是否登录
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/404') {
    // 放行
    next()
    return
  }
  // 验证cookie
  let adminName = getCookie('adminName')
  if (adminName) {
    // 设置状态
    store.commit('changeLoginStatus', true)
    // 验证vuex是否是登录状态
    if (store.state.isLogin) {
      // 如果是去首页，放行
      if (to.path === '/home') {
        next()
        return
      }
      // 验证权限
      // 将当前用户的权限获取
      let auths = sessionStorage.getItem('auths')
      // 将所有子权限路径组成一个数组
      auths = JSON.parse(auths)
      // 遍历
      let arr = []
      auths.forEach(item => {
        item.children && item.children.length && arr.push(...(item.children.map(v => v.path)))
      })
      // console.log(arr);
      // 判断to.pathshifou在数组中
      // 数to.path中有几个斜杠
      let count = 0
      let path = to.path
      for (let a = 0; a < to.path.length; a++) {
        if (to.path[a] === '/') {
          count++
          if (count > 2) {
            path = to.path.slice(0, a)
            break
          }
        }
      }
      // 判断path是否在数组中
      if (arr.includes(path)) {
        next()
      } else {
        router.push('/404')
        return
      }
      next()
      return
    } else {
      // 拦截
      router.push('/login')
      return
    }
  } else {
    // 拦截
    router.push('/login')
    return
  }
})

export default router
